import requests
import time
import random
from jsonpath import jsonpath
import json
import pymysql

'''
这里是20秒执行一次
'''
class T_X_recruit():
    def __init__(self):
        self.url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1721823285096&countryId=&cityId=&bgIds=&productId=&categoryId=40001001,40001002,40001003,40001004,40001005,40001006&parentCategoryId=&attrId=&keyword=&pageIndex=2&pageSize=10&language=zh-cn&area=cn"
        self.headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Referer': 'https://careers.tencent.com/m/search.html?query=ot_40001001,ot_40001002,ot_40001003,ot_40001004,ot_40001005,ot_40001006',
            'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36'
        }
        self.db = pymysql.connect(host='192.168.133.4', user='root', password='123456', database='self_student_jobs')
        self.cursor = self.db.cursor()  # 获取游标

    def get_html(self, url):
        response = requests.get(url=url, headers=self.headers)
        if response.status_code == 200:
            # print(response.text)
            return response.text
        raise Exception('No data~')

    def parse_data(self, json_data):
        RecruitPostNames = jsonpath(json_data, '$..RecruitPostName')  # 求职工作名
        LocationNames = jsonpath(json_data, '$..LocationName')  # 工作城市
        RequireWorkYearsNames = jsonpath(json_data, '$..RequireWorkYearsName')  # 工作经验要求
        LastUpdateTimes = jsonpath(json_data, '$..LastUpdateTime')  # 最新发布时间
        Responsibilitys = jsonpath(json_data, '$..Responsibility')  # 工作内容
        PostURLs = jsonpath(json_data, '$..PostURL')  # 工作详情链接
        BGNames = jsonpath(json_data, '$..BGName')  # 腾讯工作组别  PCG：平台与内容事业群  CDG:  企业发展事业群   WXG: 微信事业群

        for RecruitPostName, LocationName, RequireWorkYearsName, LastUpdateTime, Responsibility, PostURL, BGName in zip(
                RecruitPostNames, LocationNames, RequireWorkYearsNames, LastUpdateTimes, Responsibilitys, PostURLs,
                BGNames):
            print(f'求职工作名：{RecruitPostName}\n'
                  f'工作城市：{LocationName}\n'
                  f'工作经验要求：{RequireWorkYearsName}\n'
                  f'最新发布时间：{LastUpdateTime}\n'
                  f'工作内容：{Responsibility}\n'
                  f'工作详情链接：{PostURL}\n'
                  f'腾讯工作组别：{BGName}'
                  )
            print('=================')
            self.save_data(RecruitPostName, LocationName, RequireWorkYearsName, LastUpdateTime, Responsibility, PostURL, BGName)

    def save_data(self, RecruitPostName, LocationName, RequireWorkYearsName, LastUpdateTime, Responsibility, PostURL, BGName):
        sql = 'insert into tx_rescruit(RecruitPostName,LocationName,RequireWorkYearsName,LastUpdateTime,Responsibility,PostURL,BGName) values (%s, %s, %s, %s, %s, %s, %s)'
        self.cursor.execute(sql, (RecruitPostName, LocationName, RequireWorkYearsName, LastUpdateTime, Responsibility, PostURL, BGName))  # 执行sql语句
        self.db.commit()  # 提交
        # self.db.close()

    def main(self):
        for i in range(1, 20):
            url = f"https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1721823285096&countryId=&cityId=&bgIds=&productId=&categoryId=40001001,40001002,40001003,40001004,40001005,40001006&parentCategoryId=&attrId=&keyword=&pageIndex={i}&pageSize=10&language=zh-cn&area=cn"
            t = random.randint(20, 30)
            time.sleep(t)  # 延时请求
            html_data = self.get_html(url)
            json_data = json.loads(html_data)
            # print(json_data)
            self.parse_data(json_data)
            print(f"========下面是第{i + 1}页=========")


t_x_recruit = T_X_recruit()
t_x_recruit.main()
